import wmx from "@/common/wmx";
export default {
  install(app) {
    let tools = this.init();
    app.config.globalProperties.$tools = tools;
    app.config.globalProperties.$go = tools.go;
    app.config.globalProperties.$toast = tools.toast;
    app.config.globalProperties.$hh = tools.navbar_height();
    app.config.globalProperties.$jn_width = tools.jiaonang_width()
  },
  init() {
    let proxy = wmx.app.config.globalProperties;

    /** 获取导航栏高度*/
    const navbar_height = () => {
      // 获取系统状态栏的高度
      let height = 0;
      let systemInfo = uni.getSystemInfoSync();
      let menuButtonInfo = {};
      // #ifdef MP-WEIXIN || MP-BAIDU || MP-TOUTIAO || MP-QQ
      menuButtonInfo = uni.getMenuButtonBoundingClientRect();
      height = menuButtonInfo.bottom + 10;
      // #endif

      // #ifdef APP-PLUS || H5
      height = systemInfo.statusBarHeight + 48;
      // #endif
      return height;
    };

    const jiaonang_width = () => {
      let menuButtonInfo = 0
      // #ifdef MP
      menuButtonInfo = uni.getMenuButtonBoundingClientRect()
      // #endif
      return menuButtonInfo.width + 10
    }

    /** 跳转内置地址*/
    const go = (url) => {
      let tab_url = [
        "/pages/tabs/cart/index",
        "/pages/tabs/index/index",
        "/pages/tabs/user/index",
        "/pages/tabs/cate/index",
        "/pages/tabs/brand/index",
      ];
      if (url === "back") {
        uni.navigateBack();
        return;
      }
      if (url) {
        if (tab_url.includes(url)) {
          uni.switchTab({
            url: url,
          });
          return;
        } else {
          uni.navigateTo({
            url: url,
          });
          return;
        }
      }
    };

    /** toast提示*/
    const toast = (message) => {
      return new Promise((resolve) => {
        uni.showToast({
          title: message,
          icon: "none",
        });
        setTimeout(() => {
          resolve();
        }, 1500);
      });
    };

    // 支付
    const pay = (info) => {
      return new Promise((resolve, reject) => {
        let jsParams = info;
        uni.requestPayment({
          provider: "wxpay",
          timeStamp: jsParams.timeStamp,
          nonceStr: jsParams.nonceStr,
          package: jsParams.package,
          signType: jsParams.signType,
          paySign: jsParams.paySign,
          success: (res) => {
            resolve();
          },
          fail: (err) => {
            reject();
          },
        });
      });
    };

    return {
      navbar_height,
      jiaonang_width,
      go,
      toast,
      pay
    };
  },
};
